package cz.cvut.fel.nur.rcp;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cz.cvut.fel.nur.rcp.classes.HistoryItem;

public class NastaveniHistorie extends Activity implements OnClickListener {

	GlobalClass appState;

	Button menuUp, menuDown;

	ImageView pantry, recipes, list, options, home;

	LinearLayout labelL, removeL;

	List<ImageView> removes;
	List<TextView> labels;
	
	HistoryItem historyItemToRemove;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.nastavenihistorie);
		super.onCreate(savedInstanceState);

		appState = ((GlobalClass) getApplicationContext());

		menuDown = (Button) findViewById(R.id.menuDownButton);
		menuDown.setOnClickListener((android.view.View.OnClickListener) this);
		menuUp = (Button) findViewById(R.id.menuUpButton);
		menuUp.setOnClickListener((android.view.View.OnClickListener) this);

		pantry = (ImageView) findViewById(R.id.pantryButton);
		pantry.setOnClickListener((android.view.View.OnClickListener) this);
		recipes = (ImageView) findViewById(R.id.recipesButton);
		recipes.setOnClickListener((android.view.View.OnClickListener) this);
		list = (ImageView) findViewById(R.id.listButton);
		list.setOnClickListener((android.view.View.OnClickListener) this);
		options = (ImageView) findViewById(R.id.optionsButton);
		options.setOnClickListener((android.view.View.OnClickListener) this);
		home = (ImageView) findViewById(R.id.homeButton);
		home.setOnClickListener((android.view.View.OnClickListener) this);

		// visibility
		menuDown.setVisibility(View.GONE); // 0 is visible
		pantry.setVisibility(View.GONE);
		recipes.setVisibility(View.GONE);
		list.setVisibility(View.GONE);
		options.setVisibility(View.GONE);
		home.setVisibility(View.GONE);

		labelL = (LinearLayout) findViewById(R.id.labelLayout);
		removeL = (LinearLayout) findViewById(R.id.deleteLayout);
	}

	public void onResume() {
		super.onResume();

		generateList();
	}
	
	public void generateList() {
		DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
		DateFormat tf = new SimpleDateFormat("HH:mm");
		String lastDate = null;
		HashSet<String> dates = new HashSet<String>();
		removes = new ArrayList<ImageView>();
		labels = new ArrayList<TextView>();
		labelL.removeAllViews();
		removeL.removeAllViews();
		for (int i = appState.getHistory().size() - 1; i >= -1; i--) {
			if (i == -1) {
				TextView tvD = new TextView(this);
				tvD.setText(lastDate);
				tvD.setTextSize(20);
				tvD.setGravity(0);
				tvD.setTextColor(-16777216);
				labelL.addView(tvD, 0);

				ImageView iv = new ImageView(this);
				iv.setAdjustViewBounds(true);
				iv.setImageResource(R.drawable.minus);
				iv.setMaxHeight(38);
				iv.setMaxWidth(38);
				iv.setVisibility(View.INVISIBLE);
				removeL.addView(iv, 0);

				break;
			}
			HistoryItem h = appState.getHistory().get(i);
			if (!dates.contains(df.format(h.getDate())) && lastDate != null && !df.format(h.getDate()).equals(lastDate)) {
				TextView tvD = new TextView(this);
				tvD.setText(lastDate);
				tvD.setTextSize(20);
				tvD.setGravity(0);
				tvD.setTextColor(-16777216);
				labelL.addView(tvD, 0);

				ImageView iv = new ImageView(this);
				iv.setAdjustViewBounds(true);
				iv.setImageResource(R.drawable.minus);
				iv.setMaxHeight(38);
				iv.setMaxWidth(38);
				iv.setVisibility(View.INVISIBLE);
				removeL.addView(iv, 0);

				dates.add(df.format(h.getDate()));
			}
			TextView tv = new TextView(this);
			tv.setText(tf.format(h.getDate()) + " : " + h.getRecipe().getName());
			tv.setTextSize(16);
			tv.setGravity(0);
			tv.setPadding(15, 0, 0, 0);
			tv.setTextColor(-16777216);
			labelL.addView(tv, 0);
			labels.add(tv);

			ImageView iv = new ImageView(this);
			iv.setAdjustViewBounds(true);
			iv.setId(h.hashCode());
			iv.setClickable(true);
			iv.setImageResource(R.drawable.minus);
			iv.setMaxHeight(34);
			iv.setMaxWidth(34);
			iv.setOnClickListener((android.view.View.OnClickListener) this);
			removeL.addView(iv, 0);
			lastDate = df.format(h.getDate());
			removes.add(iv);
		}
	}

	public void onClick(View v) {
		if (v.getId() == R.id.menuDownButton) {
			menuDown.setVisibility(View.GONE);
			pantry.setVisibility(View.GONE);
			recipes.setVisibility(View.GONE);
			list.setVisibility(View.GONE);
			options.setVisibility(View.GONE);
			home.setVisibility(View.GONE);
		}
		if (v.getId() == R.id.menuUpButton) {
			menuDown.setVisibility(0);
			pantry.setVisibility(0);
			recipes.setVisibility(0);
			list.setVisibility(0);
			options.setVisibility(0);
			home.setVisibility(0);
		}

		/* obrazky */
		if (v.getId() == R.id.pantryButton) {
			startActivity(new Intent(this, Spiz.class));
		}
		if (v.getId() == R.id.recipesButton) {
			startActivity(new Intent(this, Recepty.class));
		}
		if (v.getId() == R.id.listButton) {
			startActivity(new Intent(this, NakupniSeznam.class));
		}
		if (v.getId() == R.id.optionsButton) {
			startActivity(new Intent(this, Nastaveni.class));
		}
		if (v.getId() == R.id.homeButton) {
			startActivity(new Intent(this, Main.class));
		}

		int i = 0;
		for (ImageView iv : removes) {
			if (v.getId() == iv.getId()) {
				for (HistoryItem h : appState.getHistory()) {
					if (h.hashCode() == iv.getId()) {
						

						historyItemToRemove = h;
						
						AlertDialog.Builder builder = new AlertDialog.Builder(this);
						
						builder.setMessage("Opravdu chcete odstranit recept z historie?\n(" + 
								historyItemToRemove.getRecipe().getName() + ")");
						
						// Zamena positive/negative buttonu, aby potvrzeni bylo napravo
						builder.setPositiveButton("Ponechat", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					           }
					       });
						
						builder.setNegativeButton("Odstranit", new DialogInterface.OnClickListener() {
						           public void onClick(DialogInterface dialog, int id) {
						        	   appState.getHistory().remove(historyItemToRemove);
						        	   generateList();
						           }
						       });
						
						AlertDialog dialog = builder.create();
						dialog.show();
						
						
						break;
					}
				}
			}
			i++;
		}
	}

}
